// pages/atlas_database/atlas_database.js
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commend: [],
    page: 1,
    initialState: false
  },

  getCollected: function(index) {
    var that = this,
      commend = that.data.commend;
    wx.request({
      url: app.globalData.URL + '/index.php?app=interface&mod=Collection&act=addCollection&' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        type: 213,
        id: commend[index].kmapping_id
      },
      success: function(res) {
        var _res = res.data;
        if (_res.error_code == 0) {
          commend[index].is_collection = 1;
          that.setData({
            commend: commend
          })

          wx.showToast({
            title: '收藏成功',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: _res.error_msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  getunCollected: function(index) {
    var that = this,
      commend = that.data.commend;
    wx.request({
      url: app.globalData.URL + '/index.php?app=interface&mod=Collection&act=deleteCollection&' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        type: 213,
        id: commend[index].kmapping_id
      },
      success: function(res) {
        var _res = res.data;
        if (_res.error_code == 0) {
          commend[index].is_collection = 0;
          that.setData({
            commend: commend
          })
          wx.showToast({
            title: '取消收藏成功',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: _res.error_msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  collectHandle: function(e) {
    var that = this,
      index = e.currentTarget.dataset.index,
      commend = that.data.commend;
    that.getCollected(index);
  },
  uncollectHandle: function(e) {
    var that = this,
      index = e.currentTarget.dataset.index,
      commend = that.data.commend;
    that.getunCollected(index);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.request({
      url: app.globalData.URL + '/kmapping/WebAppAjax/getKmappingListByStructureName?' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        kw: '',
        num: 10,
        wechat_flag: 1
      },
      success: function(res) {
        var _res = res.data;
        if (_res.error_code == 0) {
          var _commend = _res.data;
          that.setData({
            commend: _commend,
            initialState: true
          })
        }else {
          that.setData({
            commend: [],
            initialState: true
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this,
      _page = that.data.page + 1;
    wx.request({
      url: app.globalData.URL + '/kmapping/WebAppAjax/getKmappingListByStructureName?' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        page: _page,
        kw: '',
        num: 10,
        wechat_flag: 1
      },
      success: function(res) {
        var _res = res.data;
        if (_res.error_code == 0) {
          var _data = _res.data,
            _commend = that.data.commend.concat(_data);
          that.setData({
            'commend': _commend,
            page: _page
          })
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})